# !/usr/bin/python
# coding=utf-8
import requests
import json
from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
import base64

privateKey = 'MIICXQIBAAKBgQCIZXObErQnhu7jduBjUdZVxajwXXQ5doem3QnpO2BmIozwAZW3f9OJgrXffyKPAzOwtY0raBpWenQmGH50PXntPXhC4444kCybOHBMGMYYNsY75kJ3X0imqRlsBfp8PZdBtFb2aSZAcv3FfDwG+J/ieLIGfNQ0bmlMGsAM3vdtRwIDAQABAoGAHL/1rDw3CRXiCMQn7Ad1/EJbp76rHumjEkMJMcY4PY8O0hAjdLhW6GdK2r87k5WDMb4y8XFSC+Rn0xfan7nNlytD0mwY9RDo72MrSHAr27q/cCnnmn8wOyzK33PMbrzWmQGoOVAh0UAvZstf4W0K6WdSl36Mdoi8VpeHJeqMhnECQQCzPs7K+QyKjZCrwgEqbC1QxlTZ7hETDqGUUTyOLDfRRkOP4NvfBCFhFMDXf1YioPhK0W4crqu5OijSTmE0jhd7AkEAws10itMx/d/4uErlDXq4jfOLGtzvdNXK3zlMvGm9fLX+npnaMK1htfFW37FyEdj9fDF9jqmcCGnShfmOk39xpQJAKn8vpCvb71WVAUPj5tzy+X2sTGcHIU+Mu49vHf4Z1cIk7o3PU1a1W0ECX5VPJLKpa4Ov6Zj4GdhSKVpv7Xfq1QJBAPmjM1NofKscJ298wvW4K+P1k3EUamUuSPIxSu41D5hO3gI/pYY12n6/zZWSn1zR114dSgrVvlhlzFzWR2krq+UCQQCbGX5m4OXjoAF2P4bzDHkBR7eYvMN215NIi+bKl/qx//GUsSCMOVTGiRFvstZej4fLFyEuFyfu6gFOT9VXBLmV'
strings = 'account=18680352162&device_id=99001100000824&password=123456&req_msg_id=123&sn' \
          '=99001100000824&version=2.3.0'
# path = r'C:\Users\Administrator\Desktop\rsa_private_key.pem'
# with open(path) as f:
#     key = f.read()
key_str = '-----BEGIN PRIVATE KEY-----\n' + privateKey + '\n-----END PRIVATE KEY-----'
rsakey = RSA.importKey(key_str)
signer = Signature_pkcs1_v1_5.new(rsakey)
digest = SHA.new()
digest.update(strings.encode("utf8"))
sign = signer.sign(digest)
signature = base64.b64encode(sign)
new_sign = signature.decode('utf-8')
print(new_sign)

url = 'http://sy2.triumen.cn/user/newLogin/index'
data = {
    'device_id': '99001100000824',
    'req_msg_id': '123',
    'sn': '99001100000824',
    'sign': new_sign,
    'account': '18680352162',
    'password': '123456',
    'version': '2.3.0'
}
r = requests.post(url, data=data)
print(json.dumps(r.json(), ensure_ascii=False, indent=4))
key_str = r.json()['data']['rsa_private']
# rsakey_str = '-----BEGIN PRIVATE KEY-----\n' + key_str + '\n-----END PUBLIC KEY-----'
# print(rsakey_str)
print(key_str)
print(type(key_str))


